package com.android.support.lib.universal.view.web;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.android.support.lib.universal.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Build.VERSION;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.ZoomDensity;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.ZoomButtonsController;

public class Owebview extends RelativeLayout {

	public static int Circle = 0x01;
	public static int Horizontal = 0x02;

	private Context context;

	public SuperWebView mWebView = null; //
	private ProgressBar progressBar = null; // 水平进度条
	private RelativeLayout progressBar_circle = null; // 包含圆形进度条的布局
	private int barHeight = 3; // 水平进度条的高
	private boolean isAdd = false; // 判断是否已经加入进度条
	private int progressStyle = Horizontal; // 进度条样式,Circle表示为圆形，Horizontal表示为水平

	public Owebview(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		init();
	}

	public Owebview(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;
		init();
	}

	public Owebview(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		this.context = context;
		init();
	}

	@SuppressLint("NewApi")
	private void init() {
		mWebView = new SuperWebView(context);
		mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		if (isInEditMode()) {
			return;
		}
		WebSettings webSettings = mWebView.getSettings();
		webSettings.setJavaScriptEnabled(true);//允许JS脚本运行
		webSettings.setBuiltInZoomControls(true);



		/**
		 * 调用，实现放大缩小控件隐藏
		 */
		mWebView.getSettings().setSupportZoom(true);
		//mWebView.getSettings().setSupportMultipleWindows(true);注意这个不要设置，这个一旦设置小米等手机就无法实现url跳转了。
		mWebView.getSettings().setBuiltInZoomControls(true); // 显示放大缩小 
		mWebView.getSettings().setDefaultZoom(ZoomDensity.CLOSE);// 默认缩放模式
		/**
		 * 自适应手机屏幕，正常完整的显示页面
		 */
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.getSettings().setLoadWithOverviewMode(true);
		mWebView.getSettings().setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

		
		int sysVersion = Integer.parseInt(VERSION.SDK);
		if (sysVersion >= 11) {
			setZoomControlGoneX(mWebView.getSettings(), new Object[] { false });
		} else {
			setZoomControlGone(mWebView);
		}

		this.addView(mWebView, LayoutParams.FILL_PARENT,
				LayoutParams.FILL_PARENT);
		/**
		 * 进度条样式
		 */
		try {
			mWebView.setWebChromeClient(new WebChromeClient() {

				@Override
				public void onProgressChanged(WebView view, int newProgress) {
					// TODO Auto-generated method stub
					super.onProgressChanged(view, newProgress);
					if (newProgress == 100) {
						if (progressStyle == Horizontal) {
							if (progressBar != null) {
								progressBar.setVisibility(View.GONE);
							}

						} else {
							if (progressBar_circle != null) {
								progressBar_circle.setVisibility(View.GONE);
							}

						}
					} else {
						if (!isAdd) {
							if (progressStyle == Horizontal) {
								progressBar = (ProgressBar) LayoutInflater
										.from(context).inflate(
												R.layout.lib_webview_progress_horizontal,
												null);
								progressBar.setMax(100);
								progressBar.setProgress(0);
								Owebview.this.addView(progressBar,
										LayoutParams.FILL_PARENT, barHeight);
							} else {
								progressBar_circle = (RelativeLayout) LayoutInflater
										.from(context).inflate(
												R.layout.lib_webview_progress_circle, null);
								Owebview.this.addView(progressBar_circle,
										LayoutParams.FILL_PARENT,
										LayoutParams.FILL_PARENT);
							}
							isAdd = true;
						}

						if (progressStyle == Horizontal) {
							progressBar.setVisibility(View.VISIBLE);
							progressBar.setProgress(newProgress);
						} else {
							progressBar_circle.setVisibility(View.VISIBLE);
						}
					}
				}
			});
		} catch (Exception e) {
			// TODO: handle exception
		}

	}

	public void setBarHeight(int height) {
		barHeight = height;
	}

	public void setProgressStyle(int style) {
		progressStyle = style;
	}

	public void setClickable(boolean value) {
		mWebView.setClickable(value);
	}

	public void setUseWideViewPort(boolean value) {
		mWebView.getSettings().setUseWideViewPort(value);
	}

	public void setSupportZoom(boolean value) {
		mWebView.getSettings().setSupportZoom(value);
	}

	public void setBuiltInZoomControls(boolean value) {
		mWebView.getSettings().setBuiltInZoomControls(value);
	}

	public void setJavaScriptEnabled(boolean value) {
		mWebView.getSettings().setJavaScriptEnabled(value);
	}

	public void setCacheMode(int value) {
		mWebView.getSettings().setCacheMode(value);
	}

	public void setWebViewClient(WebViewClient value) {
		mWebView.setWebViewClient(value);
	}

	public void loadUrl(String url) {
		mWebView.loadUrl(url);
	}

	// 隐藏webview的缩放按钮 适用于3.0和以后
	public void setZoomControlGoneX(WebSettings view, Object[] args) {
		Class classType = view.getClass();
		try {
			Class[] argsClass = new Class[args.length];

			for (int i = 0, j = args.length; i < j; i++) {
				argsClass[i] = args[i].getClass();
			}
			Method[] ms = classType.getMethods();
			for (int i = 0; i < ms.length; i++) {
				if (ms[i].getName().equals("setDisplayZoomControls")) {
					try {
						ms[i].invoke(view, false);
					} catch (Exception e) {
						e.printStackTrace();
					}
					break;
				}
				// Log.e("test", ">>"+ms[i].getName());
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 隐藏webview的缩放按钮 适用于3.0以前
	public void setZoomControlGone(View view) {
		Class classType;
		Field field;
		try {
			classType = WebView.class;
			field = classType.getDeclaredField("mZoomButtonsController");
			field.setAccessible(true);
			ZoomButtonsController mZoomButtonsController = new ZoomButtonsController(
					view);
			mZoomButtonsController.getZoomControls().setVisibility(View.GONE);
			try {
				field.set(view, mZoomButtonsController);
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		}
	}
}
